package com.example.accessibilitydemo.autoclick2

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.app.Notification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Path
import android.os.Build
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.example.accessibilitydemo.R
import kotlinx.coroutines.*

class AutoClickService : AccessibilityService() {

//    https://blog.csdn.net/qq_30837235/article/details/115902248
//     此方案未经测试

    private val TAG = javaClass.canonicalName

    var mainScope: CoroutineScope? = null

    private val broadcastReceiver = BroadcastHandler(this)

    //点击间隔
    private var mInterval = -1L

    //点击坐标xy
    private var mPointX = -1f
    private var mPointY = -1f

    //悬浮窗视图
    private lateinit var mFloatingView: FloatingClickView

    companion object {

        //打开悬浮窗
        val ACTION_SHOW = "action_show"

        //自动点击事件 开启/关闭
        val ACTION_PLAY = "action_play"
        val ACTION_STOP = "action_stop"

        //关闭悬浮窗
        val ACTION_CLOSE = "action_close"
    }

    private inner class BroadcastHandler(val context: Context) : BroadcastReceiver() {

        fun register() {
            context.registerReceiver(
                this,
                IntentFilter().apply {
                    addAction(BroadcastConstants.BROADCAST_ACTION_AUTO_CLICK)
                    //息屏关闭自动点击事件
                    addAction(Intent.ACTION_SCREEN_OFF)
                }
            )
        }

        fun unregister() {
            context.unregisterReceiver(this)
        }

        @RequiresApi(Build.VERSION_CODES.N)
        override fun onReceive(p0: Context?, intent: Intent?) {
            intent?.apply {
                when(action) {
                    Intent.ACTION_SCREEN_OFF -> {
                        mFloatingView.remove()
                        mainScope?.cancel()
                    }
                    BroadcastConstants.BROADCAST_ACTION_AUTO_CLICK -> {
                        when (getStringExtra(BroadcastConstants.KEY_ACTION)) {
                            ACTION_SHOW -> {
                                mFloatingView.remove()
                                mainScope?.cancel()
                                mInterval = getLongExtra(BroadcastConstants.KEY_INTERVAL, 5000)
                                mFloatingView.show()
                            }
                            ACTION_PLAY -> {
                                mPointX = getFloatExtra(BroadcastConstants.KEY_POINT_X, 0f)
                                mPointY = getFloatExtra(BroadcastConstants.KEY_POINT_Y, 0f)
                                mainScope = MainScope()
                                autoClickView(mPointX, mPointY)
                            }
                            ACTION_STOP -> {
                                mainScope?.cancel()
                            }
                            ACTION_CLOSE -> {
                                mFloatingView.remove()
                                mainScope?.cancel()
                            }
                            else -> {
                                Log.e(TAG, "action error")
                            }
                        }
                    }
                }
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        startForegroundNotification()
        mFloatingView = FloatingClickView(this)
        broadcastReceiver.register()
    }

    private fun startForegroundNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationBuilder =
                NotificationCompat.Builder(this, NotificationConstants.CHANNEL_ID)
            val notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build()
            startForeground(-1, notification)

        } else {
            startForeground(-1, Notification())
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    private fun autoClickView(x: Float, y: Float) {
        mainScope?.launch {
            while (true) {
                delay(mInterval)
                Log.d(TAG, "auto click x:$x  y:$y")
                val path = Path()
                path.moveTo(x, y)
                val gestureDescription = GestureDescription.Builder()
                    .addStroke(GestureDescription.StrokeDescription(path, 100L, 100L))
                    .build()
                dispatchGesture(
                    gestureDescription,
                    object : AccessibilityService.GestureResultCallback() {
                        override fun onCompleted(gestureDescription: GestureDescription?) {
                            super.onCompleted(gestureDescription)
                            Log.d(TAG, "自动点击完成")
                        }

                        override fun onCancelled(gestureDescription: GestureDescription?) {
                            super.onCancelled(gestureDescription)
                            Log.d(TAG, "自动点击取消")
                        }
                    },
                    null
                )
            }
        }

    }


    override fun onInterrupt() {
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
    }

    override fun onDestroy() {
        super.onDestroy()
        broadcastReceiver.unregister()
        mainScope?.cancel()
    }

}